package io.demo01;

import java.io.*;

/**
 * Created by cyt on 2020/2/18 15:25
 */
public class ReaderFile {

    public static void main(String[] args)  throws Exception{
       ReadFile();
         //WriteFile();
        return;
    }

    /**
     *写文件的操作：
     *
     * 查看源码可得：
     *
     *   public class FileOutputStream extends OutputStream {}
     *   public abstract class OutputStream implements Closeable, Flushable {}
     *
     *   OutputStream是一个抽象类，不能被实例化，为了使用它，我们可以实例化他的子类，FileOutputStream
     *   来完成需求。
     *
     *  阅读FileOutputStream可得：
     *   1、public FileOutputStream(String name, boolean append)
     *   2、public FileOutputStream(File file, boolean append)
     *   观察构造函数，如果文件不存在会自动帮助创建文件，并且可以通过第二个参数来限定是否
     *   以追加的方式来写入。
     *
     * @throws IOException
     */
    private static void WriteFile() throws  IOException{
        OutputStream outputStream = new FileOutputStream("f:\\write.txt", true);
        String str = "使用文件读写流来写文件!!!";
        byte[] bytes= str.getBytes();
        // outputStream.write(bytes, 2, 5);

        outputStream.write(bytes);
        outputStream.close();
    }


    private static void ReadFile() throws IOException {
        // File file = new File("E:\\word");
        InputStream fileInputStream = new FileInputStream("f:\\write.txt");
        //创建缓冲
        byte[] bytes = new byte[1024];

        //读取文件
        int read = fileInputStream.read(bytes);
        while (read!=-1){
            System.out.println(""+new String(bytes, 0, read));
            read = fileInputStream.read(bytes);
        }
        fileInputStream.close();
    }
}
